package tuling.netty.splitpacket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import tuling.netty.splitpacket.domain.RgsMessageProtocol;

/**
 * @author RenGaoshuai
 * @date 2023/11/30 11:12
 * @description
 */
public class SplitPacketClientHandler extends SimpleChannelInboundHandler<RgsMessageProtocol> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for(int i = 0; i< 200; i++) {
            String msg = "你好，我是张三！";
            //创建协议包对象
            RgsMessageProtocol messageProtocol = new RgsMessageProtocol();
            messageProtocol.setLen(msg.getBytes(CharsetUtil.UTF_8).length);
            messageProtocol.setContent(msg.getBytes(CharsetUtil.UTF_8));
            ctx.writeAndFlush(messageProtocol);
        }
    }


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RgsMessageProtocol rgsMessageProtocol) throws Exception {

    }



    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
